package com.zxl.controller;

import com.zxl.constants.MQConstants;
import com.zxl.constants.RedisConstants;
import com.zxl.feign.CarFeign;
import com.zxl.feign.UserFeign;
import com.zxl.utils.PasswordUtils;
import com.zxl.vo.Customer;
import com.zxl.vo.R;
import com.zxl.vo.Sms;
import com.zxl.vo.User;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @packageName com.zxl.controller
 * @ClassName
 * @Author zxl
 * @Description //TODO
 * @date
 **/
@Controller
@RequestMapping("/user")
public class AuthController {

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CarFeign carFeign;

    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map checkUsername(String param) {
        Map<String, Object> map = new HashMap<>();
        User user = userFeign.checkUsername(param);
        if (user != null) {
            map.put("info", "用户名不可用");
            map.put("status", "n");
        } else {
            map.put("info", "用户名可用");
            map.put("status", "y");
        }
        return map;
    }

    @RequestMapping("/{page}.html")
    public String reg(@PathVariable("page") String page) {
        return page;
    }

    @RequestMapping("/sendSms/{phone}")
    @ResponseBody
    public String sendSms(@PathVariable("phone") String phone) {
        Sms sms = new Sms();
        sms.setPhone(phone);
        sms.setCode(RandomStringUtils.random(6, false, true));
        System.out.println(sms);
        rabbitTemplate.convertAndSend(MQConstants.AUTH_EXCHANGE, "auth.reg", sms);
        return "";
    }

    @RequestMapping("/reg")
    @ResponseBody
    public R reg(Customer customer) {
        //判断验证码是否正确
        String s = redisTemplate.opsForValue().get(RedisConstants.PHONE_CODE_KEY + customer.getPhone());
        if (!s.equals(customer.getCode())) {
            return R.error("验证码不正确");
        }
        //添加到数据库
        customer.setPassword(PasswordUtils.enCode(customer.getPassword()));
        userFeign.reg(customer);
        //添加成功后返回
        return R.success();
    }

    @RequestMapping("/login")
    @ResponseBody
    public R login(Customer customer, HttpSession httpSession, @CookieValue(name=RedisConstants.CAR_COOKIE,required = false)String anonId) {


        User user = userFeign.checkUsername(customer.getUsername());
        //登陆验证
        if (user == null || !PasswordUtils.check(customer.getPassword(), user.getPassword())){
            return R.error("用户名或密码错误");
        }
        customer.setId(user.getId());
        //存入redis/session
        httpSession.setAttribute(RedisConstants.USER_LOGIN_SESSION_KEY,customer);

        if (anonId != null) {
           carFeign.merge(anonId,customer.getId().toString());
        }

        return R.success();
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:http://localhost/auth/user/login.html";
    }

}
